package com.gist.cithub.user.controller;

import java.util.Arrays;
import java.util.Map;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.gist.cithub.user.entity.InformationEntity;
import com.gist.cithub.user.service.InformationService;
import com.gist.common.utils.PageUtils;
import com.gist.common.utils.R;

import javax.validation.Valid;


/**
 *
 *
 * @author lejin
 * @email sunlightcs@gmail.com
 * @date 2020-11-23 14:17:48
 */
@RestController
@RequestMapping("user/information")
public class InformationController {
    @Autowired
    private InformationService informationService;

    @RequestMapping("/getUserName/{id}")
    public R getUserNameById(@PathVariable("id") Integer id){
        String username=informationService.getById(id).getName();
        return R.ok().put("username",username);

    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = informationService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
        InformationEntity information = informationService.getById(id);

        return R.ok().put("information", information);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody InformationEntity information){
        informationService.save(information);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody InformationEntity information){
        informationService.updateById(information);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids){
        informationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }
    @RequestMapping("/test")
    public R test(){
        return R.ok().put("test","username");
    }

    @RequestMapping("/login")
    public R login(@RequestBody InformationEntity information){
        String passwordnew = DigestUtils.md5DigestAsHex(information.getPassword().getBytes());
        InformationEntity res=informationService.loginByNameAndPassword(information.getName(),passwordnew);
        return R.ok().put("user",res);
    }

    @RequestMapping("/signUp")
    public R signUp(@RequestBody InformationEntity information){
        String passwordnew = DigestUtils.md5DigestAsHex(information.getPassword().getBytes());
        information.setPassword(passwordnew);
        System.out.println(passwordnew);
        boolean res=  informationService.save(information);
        if (res){
        return R.ok();}
        else{
            return R.error();
        }
    }

}
